
//preloading.....
load("library/common.js");

JEuler.system.loadJS("library/math.js");

(function() {

    //JEuler.system.loadJS("data/???.dat");
    JEuler.system.printCaption("235");
    JEuler.system.startTimerCount();
    
    //u(k) = (900-3k)r^(k-1).
    //s(n) = Σ_(k=1...n)u(k). 
    
    //600,000,000,000
    //200,000,000,000
    //200,000,000,000/5000 = 40,000,000 = t
    //s(n) + t*5000 = Σ_(k=1...5000)(u(k)+t);
    
    function u(k, r, t) {
        return (300-k)*Math.pow(r, k-1) + t;
    }
    
    function s(n, r) {
        var t = 4*Math.pow(10,7);
        var sum=0;
        for(var i=1; i<=n; i++) {
            sum=sum+u(i, r, t);
        };
        return sum;// + 2*Math.pow(10,11);
    }
    
    var r1=1, r3=2, r2=0, rLast=0, value;
    
    do{
        rLast = r2;
        r2=(r1+r3)/2;
        value = s(5000,r2);
        value<0 ? r3=r2 : r1=r2;
    } while(!Math.abs(value)<=0.0001 && !Math.abs(rLast-r2)<=Math.pow(0.1,12));
    
    JEuler.system.print("r = " + rLast);    
    JEuler.system.print("s(5000) =" + (s(5000, rLast)*3 + 6*Math.pow(10,11)));    
    

    JEuler.system.printTimerCount();
    
})();
